You may have already worked out what is wrong from looking at the code. If so, you can give yourself a gold star and move on to the next session. If however you are mortal, you will probably not be able to see what is going wrong.

In this situation we do two things:

  • question any assumptions that we are making in the program

  • add code which allows us to prove assumptions which we are questioning

The first part of the program is supposed to move down the array looking for '*' characters. We could test that this is what is happening by adding a debug statement to print out each character it gets to. This will prove our assumption that this is what is going on.

On the right you can see my first attempt at debugging select_message. Note how I have used #define to create DEBUG. I then test to see if this symbol exists everywhere I do my debug printing. This means that I can turn the debug code off (i.e. stop it appearing in the compiled program) by simply not defining the debug character. This is a neat way of allowing you to create programs which can be built in "debug" mode or "release mode". It means that I won't have to go through the program removing debug print statements, all I have to do is not declare DEBUG.

Run project Exercise 6.3. Note what happens and try to work out why!